#ifndef _ETHTHREAD_H
#define _ETHTHREAD_H
#include <QObject>
#include <QThread>
#include <QDateTime>
#include "rtdb.h"
#include "Comstruct.h"
#include "KgGlobalData.h"
#include <QDir>
#include "mainThread.h"

class EthThread: public QThread
{
    Q_OBJECT
public:
	EthThread(QObject *parent = 0, CRTDB *pRTDB = 0, int bcIndex = 0, int ethIndex = 0, int devType = 0);
	~EthThread();
private:
	void run();	

	CRTDB *m_pRTDB;
    CBattStack *m_pBattStack;  
    devData dev;    

    void readBcmuData(int index);
    void funcBcmu(devData &dev, CBattBank *pBattBank, int index); //【BCMU与后台tcp modbus通讯点表V2.8】
    quint16 getWetCtrl(CBattBank *pBattBank);
    quint16 getDoCtrl(int &iDryCtrlNum, quint16 usDryCtrl, quint16 usDryState);
    quint16 getDoCtrl(quint16 &uDryCtrlNum, quint16 usDryCtrl, quint16 usDryState);     

    void readPcsData(int index);
    void funcPcs2030(devData &dev, CPcsData *pPcsData, int index); //【首航】【EBI系列储能变流器PCS与EMS之间的Modbus外部通讯协议V1.7】    
    
    void readUpsData(int index);
    void funcUps2030(devData &dev, CUpsData *pUpsData, int index); //【MODBUS_UPS_C_v1.0】

    void readBmsData(int index);
    void funcBms2090(devData &dev, int index); //【奥威】【PCS与BMS通信点表】

    QString m_serverIP;
    quint16 m_serverPort;   
    quint16 m_uMbap;   

    bool m_bOpen;
	int m_bcIndex;
    int m_ethIndex;
    int m_devType;
	int ret;  
    quint8 temp8;
	quint16 temp16;     
    quint32 temp32;
    float tempf;

    char m_pSend[512];
    char m_pRecv[512];       
    QTcpSocket *m_tcpSocket;  
    int tcpInit(QString m_serverIP, quint16 m_serverPort);   
	int readRegData(quint8 devAddr,quint8 funCode,quint16 startAddr,quint16 regCnt);
    int setSingleRegData(quint8 devAddr,quint8 funCode,quint16 startAddr,quint16 regValue);   
	int setMultipleRegData(quint8 devAddr,quint8 funCode,quint16 startAddr,quint16 regCnt,quint16 *dataBuf);

    int m_isOutputLog;    
 	KgLogFile m_logFile; 
    QString m_strLog;      
    void devConfig(int devType); 
    void writeKgLogfile(QString strInfo,bool bSaveToFile=false);    
};

#define KG_MAX_LOG_SIZE     1024*1024*10
#define KG_LOG_INFO(MESSAGE)\
	do{ \
        QString strInfor;\
        QString currentFileName = QFileInfo(__FILE__).fileName();\
        QString currentFileLine = QString::number(__LINE__);\
        strInfor = currentFileName + "(" + currentFileLine + ") " + MESSAGE;\
        writeKgLogfile(strInfor,true);\
	} while(0)
	
#endif
